package main

import (
	"flag"
	"fmt"
	"net/http"

	"gitee.com/gao-deguang/easy-chat/apps/im/ws/internal/config"
	"gitee.com/gao-deguang/easy-chat/apps/im/ws/internal/handler"
	"gitee.com/gao-deguang/easy-chat/apps/im/ws/internal/handler/user"
	"gitee.com/gao-deguang/easy-chat/apps/im/ws/internal/svc"
	"gitee.com/gao-deguang/easy-chat/apps/im/ws/websocket"
	"gitee.com/gao-deguang/easy-chat/pkg/constant"

	"github.com/zeromicro/go-zero/core/conf"
)

var configFile = flag.String("f", "etc/dev/im.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	if err := c.SetUp(); err != nil {
		panic(err)
	}
	//todo

	ctx := svc.NewServiceContext(c)
	srv := websocket.NewServer(c.ListenOn,
		websocket.WithAuthentication(user.NewJwtAuth(ctx)),
		websocket.WithMaxConnectionIdle(0),
		websocket.WithAck(websocket.OnlyAck, 0, 5),
		websocket.WithServerDiscover(websocket.NewMyDiscover(c.ListenOn, constant.REDIS_SERVER_DISCOVER_KEY, http.Header{}, c.Redisx)),
	)
	defer srv.Stop()

	handler.RegisterHandlers(srv, ctx)

	fmt.Printf("Starting websocket server at %v ...\n", c.ListenOn)

	srv.Start()

}
